import opentracing
from apptrack.span import Span

class ScopeManager(opentracing.ScopeManager):
    def __init__(self):
        self.active_scope = None
        
    def activate(self, span, finish_on_close):
        self.active_scope = Scope(self,span)
        span.set_tag('active',True)
        return self.active_scope
        
    @property
    def active(self):
        return self.active_scope
        

    
class Scope(opentracing.Scope):
    """description of class"""
    def __init__(self, manager, span):
        opentracing.Scope.__init__(self,manager, span)
        
    def close(self):
        self.span.set_tag('active',False)
        self.span.finish()
        
    def __exit__(self, exc_type, exc_val, exc_tb):
        """Calls :meth:`close()` when the execution is outside the Python
        Context Manager.

        If exception has occurred during execution, it is automatically logged
        and added as a tag to the :class:`Span`.
        :attr:`~operation.ext.tags.ERROR` will also be set to `True`.
        """
        Span._on_error(self.span, exc_type, exc_val, exc_tb)
        self.close()
